package development;

import state.AlertListener;
import state.DrawListener;
import state.GameForm;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.Sprite;
import state.TimerListener;
import state.component.BottomBar;
import state.component.Event;
import state.component.EventListener;
import state.component.GameMenu;
import state.component.GridMenu;
import state.font.FontManager;
import state.font.ImageFont;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.array.ObjectArray;
import development.bean.Game;
import development.group.FrmGroupMenu;
import development.socket.GameActionHandle;
import development.socket.SocketClientUtil;

public class FrmChooseGame extends GameForm implements EventListener, AlertListener, TimerListener {
  private int HOZ_DISTANCE;
  private int VER_DISTANCE;
  private int DX;
  private int DY;
  private int NUMBER_OF_ITEM_PER_ROW;
  
  private ObjectArray itemsImages;
  private GridMenu gridMenu;
  private boolean isSendUserCountRequest = false;  
  
  private Sprite userOnlineSprite;
  private ImageFont text8;
  private GameImage onlineImage;

  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmChooseGame();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    isSendUserCountRequest = false;
    // Reset user count
    for (int i = 0; i < Constants.GAME_USER_COUNT.len(); i++) {
      Constants.GAME_USER_COUNT.set(i, 0);
    }
    
    itemsImages = ObjectArray.create(Constants.GAME_ID.len());
    for (int i = 0; i < itemsImages.len(); i++) {
      itemsImages.set(i, ImageUtil.getImage(Constants.GAME_ICON_CATEGORY, Constants.GAME_ICON.get(i)));
    }

    if (Constants.IS_240x320_SCREEN) {
      HOZ_DISTANCE = 70;
      VER_DISTANCE = 60;
      DX = 50;
      DY = 60;
      NUMBER_OF_ITEM_PER_ROW = 3;
    } else {
      HOZ_DISTANCE = 70;
      VER_DISTANCE = 60;
      DX = 50;
      DY = 60;
      NUMBER_OF_ITEM_PER_ROW = 4;
    }
    
    // Create grid menu
    gridMenu = new GridMenu(_manager, itemsImages, Constants.GAME_NAME, NUMBER_OF_ITEM_PER_ROW, DX, DY, HOZ_DISTANCE, VER_DISTANCE, this);
    gridMenu.setToolTip(Constants.GAME_TOOLTIP);
    gridMenu.setEventNumbers(Constants.GAME_USER_COUNT);
    gridMenu.setForceGroundColor(GameColor.WHITE_CODE);
    gridMenu.setFocusFgColor(GameColor.YELLOW_CODE);
    gridMenu.setEnableScroll(false);
    setTimerListener(this);
    
    // Display number of user online
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    onlineImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "icon_online.png");
    userOnlineSprite = new Sprite(null, _manager, Constants.SCREEN_WIDTH - 80, 20, Constants.TOP_LEFT_ANCHOR);
    userOnlineSprite.setSize(70, 20);
    userOnlineSprite.setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        if (Global.numberOfUserOnline > -1) {
          String txt = "Online: " + Global.numberOfUserOnline;
          g.drawImage(onlineImage, Constants.SCREEN_WIDTH - text8.stringWidth(txt) - 23, 29, Constants.VCENTER_LEFT_ANCHOR);
          text8.setBold(true);
          text8.drawString2(g, txt, GameColor.YELLOW_CODE, Constants.SCREEN_WIDTH - text8.stringWidth(txt) - 10, 27, Constants.VCENTER_LEFT_ANCHOR);
          text8.setBold(false);
        }
      }
    });
  }
  
  public GridMenu getGridMenu() {
    return gridMenu;
  }
  
  public void draw(Graphics g) {
    DrawUtils.getBottomBar(FrmGroupMenu.instance().getManager()).setRightLabel("Trở về");
    DrawUtils.drawBackground(g);
  }
  
  public void doTask() {
    if (GameActionHandle.isLoginSuccess && Global.gameActionClient.isConnectSuccess && !isSendUserCountRequest) {
      isSendUserCountRequest = true;
      SocketClientUtil.outAllGameNotifyRequest();
    }
  }
  
  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.FIRE:
      BottomBar bottomBar = DrawUtils.getBottomBar(FrmGroupMenu.instance().getManager());
      if (bottomBar != null) {
        GameMenu leftMenu = bottomBar.getLeftMenu();
        if (leftMenu != null && !leftMenu.isShowing()) {
          chooseMenu(gridMenu.getCurrentSelectedIndex());
        }
      }
      break;
    }
  }
  
  private void chooseMenu(int choosedIndex) {
    String gameId = Constants.GAME_ID.get(choosedIndex);
    if (Global.gameActionClient == null || !Global.gameActionClient.isConnectSuccess) {
      return;
    }
    
    Global.currentGame = (Game) Global.gameMap.get(gameId);
    SocketClientUtil.joinGameRequest(gameId);
    Global.alert.showCustomUIAlert(FrmMenuGame.instance(), 0);
  }

  public void onActionPerform(Event event) {
    if (event.getSource() == gridMenu) {
      chooseMenu(gridMenu.getCurrentSelectedIndex());
      return;
    }
  }

  public void destroy() {
    itemsImages = null;
    gridMenu.detroy();
    gridMenu = null;
    singleton = null;
    onlineImage = null;
  }
}
